package goodbaby.dklt.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.classic.core.fragment.BaseFragment;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;
import com.squareup.picasso.Picasso;
import com.tgd.gbb.uikit.ui.widget.MyListView;
import com.tgd.gbb.uikit.ui.widget.asyncimageview.CircleImageView;

import java.lang.reflect.Type;
import java.util.List;

import goodbaby.dklt.R;
import goodbaby.dklt.activity.AttendanceActivity;
import goodbaby.dklt.activity.LoginActivity;
import goodbaby.dklt.adapter.CommonAdapter;
import goodbaby.dklt.adapter.ViewHolder;
import goodbaby.dklt.bean.AttendanceClassBean;
import goodbaby.dklt.bean.ChildrenBean;
import goodbaby.dklt.bean.ClassBean;
import goodbaby.dklt.bean.ClassListInfo;

/**
 * Created by Li.z on 2016/5/7.
 * 班级fragment
 */
public class ClassFragment extends BaseFragment {
    private TextView tvTotalCount;
    private TextView tvAttendanceCount;
    private TextView tvLeaveCount;

    private GridView gridView;

    private Dialog mDialog;
    private MyListView listView;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_class;
    }

    @Override
    public void initView(View parentView) {
        super.initView(parentView);

        tvTotalCount = (TextView) parentView.findViewById(R.id.tv_total_count);
        tvAttendanceCount = (TextView) parentView.findViewById(R.id.tv_attendance_count);
        tvLeaveCount = (TextView) parentView.findViewById(R.id.tv_leavel_count);
        gridView = (GridView) parentView.findViewById(R.id.gv_gridview);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ChildrenBean c = ((ClassStudentAdapter) gridView.getAdapter()).getItem(position);
                showDialog(c);
            }
        });
    }

    public void loadData(String classId) {

        ActionHelp.gbbAttendanceClass(activity, classId, new ObjectCallback<AttendanceClassBean>() {
            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                }
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                ToastUtil.showToast(activity, "失败");
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(activity, "失败");
            }

            @Override
            public void onSuccess(AttendanceClassBean response) {

                tvTotalCount.setText(Html.fromHtml("全班" + response.getChildrenCount() + "人"));
                tvAttendanceCount.setText(Html.fromHtml("实到" + response.getAttendanceCount() + "人"));
                tvLeaveCount.setText(Html.fromHtml("请假" + response.getLevelCount() + "人"));

                gridView.setAdapter(new ClassStudentAdapter(activity, response.getClasschildrenlist()));

            }

            @Override
            public Type getType() {
                return new TypeToken<AttendanceClassBean>() {
                }.getType();
            }
        });

    }

    private void showDialog(ChildrenBean children) {
        if (mDialog == null) {
            mDialog = new Dialog(activity, R.style.common_dialog);
            mDialog.setCanceledOnTouchOutside(true);
            View contentView = View.inflate(activity, R.layout.dialog_student_detail, null);
            listView = (MyListView) contentView.findViewById(R.id.mlv_listview);
            mDialog.setContentView(contentView);
            // mDialog.addContentView(contentView);
        }
        listView.setAdapter(new StudentDetailAdapter(activity, children));
        mDialog.show();
    }

    private void dismissDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    private class ClassStudentAdapter extends CommonAdapter<ChildrenBean> {

        public ClassStudentAdapter(Context context, List<ChildrenBean> mDatas) {
            super(context, R.layout.item_grid_calss_student, mDatas);
        }

        @Override
        public void convert(ViewHolder helper, ChildrenBean item, int position) {
            helper.setText(R.id.tv_username, item.getName());

            CircleImageView image = helper.getView(R.id.civ_head_image);
            if (!TextUtils.isEmpty(item.getImagePath())) {
                Picasso.with(mContext).load(item.getImagePath()).fit().into(image);
            } else {
                image.setImageResource(R.mipmap.top_icon_userimg);
            }

        }
    }

    private class StudentDetailAdapter extends CommonAdapter<ChildrenBean> {
        String key = "";
        String value = "";
        TextView tvKey;
        TextView tvValue;
        private ChildrenBean children;

        public StudentDetailAdapter(Context context, ChildrenBean children) {
            super(context, R.layout.item_list_student_detail, null);
            this.children = children;
        }

        @Override
        public void convert(ViewHolder helper, ChildrenBean item, int position) {
            tvKey = helper.getView(R.id.tv_key);
            tvValue = helper.getView(R.id.tv_value);

            tvKey.setTextColor(Color.parseColor("#666666"));
            tvValue.setTextColor(Color.parseColor("#666666"));

            switch (position) {
                case 0:
                    key = "姓名:";
                    value = item.getName();
                    break;
                case 1:
                    key = "昵称:";
                    value = item.getNICKNAME();
                    break;
                case 2:
                    key = "性别:";
                    value = "1".equals(item.getSex()) ? "男" : "女";
                    break;
                case 3:
                    key = "年龄:";
                    value = item.getAGE() + "";
                    break;
                case 4:
                    key = "出生日期:";
                    value = item.getBirthday();
                    break;
                case 5:
                    key = "家长姓名:";
                    value = item.getParentName();
                    break;
                case 6:
                    key = "家长电话:";
                    value = item.getParentPhone();
                    tvKey.setTextColor(Color.parseColor("#55A0C3"));
                    tvValue.setTextColor(Color.parseColor("#55A0C3"));
                    break;
                case 7:
                    key = "现住地:";
                    value = item.getResidence();
                    break;
                case 8:
                    key = "原住地:";
                    value = item.getRawResidence();
                    break;
                case 9:
                    key = "是否常住:";
                    value = "1".equals(item.getIsStay()) ? "常住" : "暂住";
                    break;
            }

            tvKey.setText(key);
            tvValue.setText(value);
        }

        @Override
        public ChildrenBean getItem(int position) {
            return children;
        }

        @Override
        public int getCount() {
            return 10;
        }
    }
}
